const url = require('url');
const useSocket = (io) => {
	io.on('connection', (socket) => {
		console.log('websocket has connected');
		const roomid = url.parse(socket.request.url, true).query.roomid;
		const ipAddress = socket.handshake.address;
		socket.join(roomid);
		const count = io.engine.clientsCount;
		io.to(roomid).emit('message', { data: `欢迎连接,当前在线人数${count}人` });

		socket.on('disconnecting', (res) => {
			//用户离开
			console.log(roomid); // the Set contains at least the socket ID
			io.to(roomid).emit('message', { data: `欢迎连接,当前在线人数${io.engine.clientsCount}人` });
		});

		socket.on('say', function (data) {
			console.log(data);
			const {name,msg} = data
			socket.broadcast.to(roomid).emit('news', {name,msg});
		});
	});
};
module.exports = useSocket;
